package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.entity.Order;
import com.mall.service.OrderService;
import com.mall.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端订单管理
 */
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 分页获取订单列表
     */
    @GetMapping("/list")
    @AuthCheck
    public Result<List<OrderVO.OrderListVO>> listOrders(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status
    ) {
        // TODO: 实现分页和状态筛选
        List<OrderVO.OrderListVO> list = orderService.listOrders(pageNum, pageSize, status);
        return Result.success(list);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    @AuthCheck
    public Result<OrderVO.OrderDetailVO> getOrder(@PathVariable Long orderId) {
        // 管理端无需 userId，直接查详情
        OrderVO.OrderDetailVO detail = orderService.getOrderDetailByAdmin(orderId);
        return Result.success(detail);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{orderId}")
    @AuthCheck
    public Result<String> deleteOrder(@PathVariable Long orderId) {
        orderService.deleteOrderByAdmin(orderId);
        return Result.success("删除成功");
    }

    /**
     * 编辑订单（仅允许修改收货信息和备注）
     */
    @PutMapping("/{orderId}")
    @AuthCheck
    public Result<String> updateOrder(@PathVariable Long orderId, @RequestBody Order order) {
        order.setId(orderId);
        orderService.updateOrderByAdmin(order);
        return Result.success("更新成功");
    }
}